/**
 * RequestParamTrimWrapper.java
 * Created at 2016年8月23日
 * Created by Jackie Liu
 * Copyright (C) 2016, All rights reserved.
 */
package com.huabo.interceptor;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * <p>ClassName: RequestParamTrimWrapper</p>
 * <p>Description: 对请求参数去除前后空格并转义html4特殊字符</p>
 * <p>Author: Jackie Liu</p>
 * <p>Date: 2016年8月23日</p>
 */
public class RequestParamTrimWrapper extends HttpServletRequestWrapper {

	/**
	 * <p>Description: 构造参数</p>
	 * @param request
	 */
	public RequestParamTrimWrapper(HttpServletRequest request) {
		super(request);
	}

	/* (non-Javadoc)
	 * <p>Title: getParameter</p>
	 * <p>Description: </p>
	 * @param name
	 * @return
	 * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String)
	 */
	@Override
	public String getParameter(String name) {
		String value = super.getParameter(name);
		if (StringUtils.isNotEmpty(value)) {
			return value.trim();
		}
		return value;
	}

	/* (non-Javadoc)
	 * <p>Title: getParameterValues</p>
	 * <p>Description: </p>
	 * @param name
	 * @return
	 * @see javax.servlet.ServletRequestWrapper#getParameterValues(java.lang.String)
	 */
	@Override
	public String[] getParameterValues(String name) {
		String[] values = super.getParameterValues(name);
		if (values != null) {
			for (int i = 0; i < values.length; i ++) {
				final String value = values[i];
				if (StringUtils.isNotEmpty(value)) {
					//values[i] = StringEscapeUtils.escapeHtml4(value.trim());
					values[i] = value.trim();
				}
			}
		}
		return values;
	}
	
}
